package dianmobile.eistarandroid.network.request;

import android.content.Context;

import com.android.volley.Response;

import org.xmlpull.v1.XmlPullParser;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import dianmobile.eistarandroid.beans.BoardItemEntity;
import dianmobile.eistarandroid.utils.ConstantsData;

/**
 * Created by admin on 15/7/16.
 */
public class SubBoardRequest extends ByhhAndroidRequest {

    public static void execute(Context context, RequestCallback callback, String groupName) {
        Map<String, String> params = new HashMap<>();
        if (ConstantsData.GROUP_EISTAR.equals(groupName))
            params.put("group", "0=0");
        else if (ConstantsData.GROUP_EI.equals(groupName))
            params.put("group", "1=1");
        else if (ConstantsData.GROUP_DIGITAL.equals(groupName))
            params.put("group", "2=2");
        else if (ConstantsData.GROUP_SCIENCE.equals(groupName))
            params.put("group", "3=3");
        else if (ConstantsData.GROUP_ART.equals(groupName))
            params.put("group", "4=4");
        else if (ConstantsData.GROUP_SINCERE.equals(groupName))
            params.put("group", "5=5");
        else if (ConstantsData.GROUP_ENTERTAINMENT.equals(groupName))
            params.put("group", "6=6");
        else if (ConstantsData.GROUP_NEWS.equals(groupName))
            params.put("group", "7=7");
        executeSuper(context, RequestType.SUB_BOARD, params, callback);
    }

    public SubBoardRequest(int method, String url, Response.ErrorListener listener) {
        super(method, url, listener);
    }
    public SubBoardRequest(int method, String url, Map<String, String> params,
                        RequestCallback callback, Response.ErrorListener listener) {
        super(method, url, params, callback, listener);
    }

    @Override
    protected void parseXmlToMap(XmlPullParser parser, Map<String, Object> map) {
        List<BoardItemEntity> boardItemEntities = new ArrayList<>();
        BoardItemEntity boardItemEntity = null;

        final String SU_BOARD = "board";
        final String SU_ENAME = "ename";
        final String SU_CNAME = "cname";

        try {
            int even = parser.getEventType();
            while (even != XmlPullParser.END_DOCUMENT) {
                switch (even) {
                    case XmlPullParser.START_DOCUMENT:

                        break;

                    case XmlPullParser.START_TAG:
                        String tagName = parser.getName();
                        if (SU_BOARD.equals(tagName))
                            boardItemEntity = new BoardItemEntity();
                        else if (SU_ENAME.equals(tagName))
                            boardItemEntity.setBoardName(parser.nextText());
                        else if (SU_CNAME.equals(tagName))
                            boardItemEntity.setBoardTitle(parser.nextText());

                        break;

                    case XmlPullParser.END_TAG:
                        String endTagName = parser.getName();
                        if (SU_BOARD.equals(endTagName)) {
                            boardItemEntities.add(boardItemEntity);
                            boardItemEntity = null;
                        }

                        break;
                }

                even = parser.next();
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
        }

        map.put("result_data", boardItemEntities);
    }
}
